//
// files.cpp
// libprotea
// Tools Library for the Protea Project / File management functionality
// Copyright (C) 2003, 2004, 2005 eXegeSys, Inc.
// Copyright (C) 2008 Bruce A. James
//
// This program is part of the Protea Project.
//
// The Protea Project is free software; you can redistribute it and/or modify it 
// under the terms of the GNU General Public License as published by the 
// Free Software Foundation, either version 3 of the License, or (at your 
// option) any later version.
//
// The Protea Project is distributed in the hope that it will be useful, but 
// WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY 
// or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 
// for more details.
// 
// You should have received a copy of the GNU General Public License along 
// with this program.  If not, see <http://www.gnu.org/licenses/>.
// 
// Please send correspondence to:
// 
// theproteaproject@gmail.com
//

#include "../defines.h"
#include "files.h"
#include "../AppHandler/AppInterface.h"

void DumpMemory( char* data, int count ) {
	const long cutoff = 20;

	int i, j;
	unsigned char temp[ cutoff + 1 ];
	char temp2[ 256 ];
	int position = 0;

	while (count) {
		int mylen;

		if (count > cutoff)
			mylen = cutoff;
		else
			mylen = count;

		memcpy( temp, &data[ position ], mylen );
		if (mylen < cutoff)
			memset( &temp[ mylen ], 0, cutoff - mylen );

		// Create the hex version
		j = 0;
		for (i=0; i<cutoff; i++) {
			if (i < mylen) {
				j += sprintf( &temp2[j], "%2.2X ", temp[i] );
			} else
				j += sprintf( &temp2[j], "   " );
		};

		// Fix non-printable characters
		for (i=0; i<mylen; i++) {
			if (temp[i] == 0)
				temp[i] = '~';
			else if (temp[i] < 32)
				temp[i] = 32;
		};
		temp[mylen] = 0;

		// Output everything
		printf( "  %4d: %s %s\n", position, temp2, temp );

		count -= mylen;
		position += mylen;
	};
};

int XCRenameFile( const char* source, const char* dest ) {
	return rename( source, dest );
};

XCFileTransport::XCFileTransport( XCAppInterface* appowner, XCMsgPort* portowner ) 
	: XCTransport( appowner, portowner )
{
	file = (PORT_HANDLE)0;
	dirty_cache = false;
	inbuffer = NULL;
	outbuffer = NULL;
	inleft = 0;
	outused = 0;
	outstart = 0;
	instart = 0;
	eof_flag = false;
};

XCFileTransport::~XCFileTransport() {
	Close();
};

void XCFileTransport::CancelIO() {
};

PORT_HANDLE XCFileTransport::GetInHandle() {
	return file;
};

PORT_HANDLE XCFileTransport::GetOutHandle() {
	return file;
};

